//
//  RightDetailView.m
//  Daybook
//
//  Created by dllo on 16/5/28.
//  Copyright © 2016年 zhangbin. All rights reserved.
//

#import "RightDetailView.h"
#import "Common.h"
#import "RightDetailViewCell.h"

#define TEXT_COLOR [UIColor lightGrayColor]

@interface RightDetailView ()
<
    DatePickerViewDelegate,
    UITableViewDataSource,
    UITableViewDelegate
>

@property (nonatomic, strong)UILabel *percentageTitleLabel;

@property (nonatomic, strong)UILabel *nameTitleLabel;

@property (nonatomic, strong)UILabel *moneyTitleLabel;



@property (nonatomic, strong) BaseView *navigationView;
@property (nonatomic, strong) UIButton *toButton;
@property (nonatomic, strong) UIButton *beginDateButton;
@property (nonatomic, strong) UIButton *endDateButton;
@property (nonatomic, strong) UIButton *leftImageButton;
@property (nonatomic, strong) UIButton *rightImageButton;

@property (nonatomic, strong) DatePickerView *datePickerView;
@property (nonatomic, strong) NSMutableDictionary *datePickerDic;

@property (nonatomic, strong)UILabel *topTitleLabel;

@property (nonatomic, strong)UITableView *tableView;

@property (nonatomic, strong)UILabel *percentageLabel;

@property (nonatomic, strong)UILabel *nameLabel;

@property (nonatomic, strong)UILabel *moneyLabel;

@property (nonatomic, strong) NSDate *beginDate;
@property (nonatomic, strong) NSDate *endDate;

@property (nonatomic, strong) NSMutableArray *rightDetailArr;

@property (nonatomic, strong) UILabel  *sectionDateLabel;
@property (nonatomic, strong) UILabel *sectionCountLabel;

@end

@implementation RightDetailView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.rightDetailArr = [NSMutableArray array];
        self.datePickerDic = [NSMutableDictionary dictionary];
        self.beginDate = [[CalendarTool shareCalendarTool] firstDayFromDate:[NSDate date]];
        self.endDate = [[CalendarTool shareCalendarTool] lastDayFromDate:[NSDate date]];
        [self createTopView];
        [self createView];
        [self createTableView];
    }
    return self;
}

- (void)setTopModel:(ChartModel *)topModel {
    _topModel = topModel;
    _topTitleLabel.text = topModel.name;
    _percentageLabel.text = topModel.proportionNum;
    _nameLabel.text = topModel.name;
    _moneyLabel.text = topModel.amount;
    
    [self createData];
}

- (void)createData {
    _rightDetailArr = [[RightDetailTool shareRightDetailTool] modelArrayFromModel:_topModel beginDate:_beginDate endDate:_endDate];
    [_tableView reloadData];
}

- (void)createView {
   
    self.navigationView = [[BaseView alloc] initWithFrame:CGRectMake(0, 80 * MAIN_HEIGHT / 736.0 - 1, MAIN_WIDTH, 1)];
    [self addSubview:_navigationView];
    self.navigationView.backgroundColor = LINE_COLOR;
}

- (void)createTopView {
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(10 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (40 / 736.0)), 25 / 414.0 * MAIN_WIDTH, 25 / 414.0 * MAIN_WIDTH);
    [self addSubview:backButton];
    backButton.backgroundColor = [UIColor clearColor];
    [backButton setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(clickBack:) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    self.topTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 60 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH)];
    self.topTitleLabel.center = CGPointMake(MAIN_WIDTH / 2, (MAIN_HEIGHT * (50 / 736.0)));
    _topTitleLabel.font = [UIFont systemFontOfSize:17 / 414.0 * self.frame.size.width];

    
    self.topTitleLabel.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.topTitleLabel];
    self.topTitleLabel.textAlignment = 1;
    
    
    self.percentageLabel = [[UILabel alloc]initWithFrame:CGRectMake(17 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (150 / 736.0)), 90 / 414.0 * MAIN_WIDTH, 20 / 414.0 * MAIN_WIDTH)];
    self.percentageLabel.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.percentageLabel];
    _percentageLabel.textAlignment = NSTextAlignmentCenter;
    
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(MAIN_WIDTH / 2 - 80 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (150 / 736.0)), 150 / 414.0 * MAIN_WIDTH, 20 / 414.0 * MAIN_WIDTH)];
    self.nameLabel.backgroundColor = [UIColor whiteColor];
    _nameLabel.center = CGPointMake(MAIN_WIDTH / 2, (MAIN_HEIGHT * (150 / 736.0)) + 10 / 414.0 * MAIN_WIDTH);
    [self addSubview:self.nameLabel];
    self.nameLabel.textAlignment = 1;
    
    self.moneyLabel = [[UILabel alloc]initWithFrame:CGRectMake(MAIN_WIDTH - 115 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (150 / 736.0)), 110 / 414.0 * MAIN_WIDTH, 20 / 414.0 * MAIN_WIDTH)];
    self.moneyLabel.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.moneyLabel];
    self.moneyLabel.textAlignment = 1;
    
    self.percentageTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(20 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (180 / 736.0)), 85 / 414.0 * MAIN_WIDTH, 20 / 414.0 * MAIN_WIDTH)];
    self.percentageTitleLabel.backgroundColor = [UIColor whiteColor];
    self.percentageTitleLabel.textAlignment = 1;
    self.percentageTitleLabel.text = @"消费占比";
    self.percentageTitleLabel.textColor = TEXT_COLOR;
    self.percentageTitleLabel.font = [UIFont systemFontOfSize:13];
    [self addSubview:self.percentageTitleLabel];
    
    
    self.nameTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(MAIN_WIDTH / 2 - 30 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (180 / 736.0)), 60 / 414.0 * MAIN_WIDTH, 20 / 414.0 * MAIN_WIDTH)];
    self.nameTitleLabel.backgroundColor = [UIColor whiteColor];
    self.nameTitleLabel.textAlignment = 1;
    self.nameTitleLabel.text = @"分类";
    self.nameTitleLabel.textColor = TEXT_COLOR;
    self.nameTitleLabel.font = [UIFont systemFontOfSize:13];
    [self addSubview:self.nameTitleLabel];
    
    self.moneyTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(MAIN_WIDTH - 90 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (180 / 736.0)), 60 / 414.0 * MAIN_WIDTH, 20 / 414.0 * MAIN_WIDTH)];
    self.moneyTitleLabel.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.moneyTitleLabel];
    self.moneyTitleLabel.text = @"总消费";
    self.moneyTitleLabel.textAlignment = 1;
    self.moneyTitleLabel.font = [UIFont systemFontOfSize:13];
    self.moneyTitleLabel.textColor = TEXT_COLOR;
    
    
    
    
    
//    self.navigationView = [[BaseView alloc] initWithFrame:CGRectMake(0, 80 * MAIN_HEIGHT / 736.0 - 1, MAIN_WIDTH, 1)];
//    [self addSubview:_navigationView];
//    self.navigationView.backgroundColor = LIGHT_GRAY;
    
    
    self.beginDateButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(0, 0, 60 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH);
        tempButton.center = CGPointMake(MAIN_WIDTH / 2 - 45 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (100 / 736.0)));
        [self addSubview:tempButton];
        [tempButton setTitle:[[CalendarTool shareCalendarTool] getMonthAndDayStringFromDate:_beginDate] forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(popDateViewAction:) forControlEvents:UIControlEventTouchUpInside];
        //        tempButton.backgroundColor = [UIColor lightGrayColor];
        //        tempButton.layer.borderWidth = 1;
        //        tempButton.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        [tempButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        tempButton.titleLabel.font = [UIFont systemFontOfSize:16];
        tempButton;
    });
    
    self.endDateButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(0, 0, 60 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH);
        tempButton.center = CGPointMake(MAIN_WIDTH / 2  + 45 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (100 / 736.0)));
        [self addSubview:tempButton];
        [tempButton setTitle:[[CalendarTool shareCalendarTool] getMonthAndDayStringFromDate:_endDate] forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(popDateViewAction:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        tempButton.titleLabel.font = [UIFont systemFontOfSize:16];
        //        tempButton.backgroundColor = [UIColor lightGrayColor];
        //        tempButton.layer.borderWidth = 1;
        //        tempButton.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        tempButton;
    });
    
    self.toButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(0, 0, 60 / 414.0 * MAIN_WIDTH, 30 / 414.0 * MAIN_WIDTH);
        tempButton.center = CGPointMake(MAIN_WIDTH / 2, (MAIN_HEIGHT * (100 / 736.0)));
        [self addSubview:tempButton];
        [tempButton setTitle:@"至" forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(popDateViewAction:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //        tempButton.backgroundColor = [UIColor lightGrayColor];
        //        tempButton.layer.borderWidth = 1;
        //        tempButton.layer.borderColor = [[UIColor lightGrayColor]CGColor];
        tempButton;
    });
    
    self.leftImageButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(0, 0, 15 / 414.0 * MAIN_WIDTH, 15 / 414.0 * MAIN_WIDTH);
        tempButton.center = CGPointMake(MAIN_WIDTH / 2  - 95 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (100 / 736.0)));
        [self addSubview:tempButton];
        [tempButton setImage:[UIImage imageNamed:@"fanhui.png"] forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(popDateViewAction:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        tempButton;
    });
    
    self.rightImageButton = ({
        UIButton *tempButton = [UIButton buttonWithType:UIButtonTypeCustom];
        tempButton.frame = CGRectMake(0, 0, 15 / 414.0 * MAIN_WIDTH, 15 / 414.0 * MAIN_WIDTH);
        tempButton.center = CGPointMake(MAIN_WIDTH / 2  + 90 / 414.0 * MAIN_WIDTH, (MAIN_HEIGHT * (100 / 736.0)));
        [self addSubview:tempButton];
        [tempButton setImage:[UIImage imageNamed:@"xiangqian.png"] forState:UIControlStateNormal];
        [tempButton addTarget:self action:@selector(popDateViewAction:) forControlEvents:UIControlEventTouchUpInside];
        [tempButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        tempButton;
    });
    
    

    
    
}

- (void)createTableView {
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, (MAIN_HEIGHT * (230 / 736.0)), MAIN_WIDTH,MAIN_HEIGHT - (MAIN_HEIGHT * (230 / 736.0))) style:UITableViewStylePlain];
    
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.dataSource = self;
    _tableView.delegate = self;
//    _tableView.rowHeight = 60;
    [self addSubview:_tableView];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [_tableView registerClass:[RightDetailViewCell class] forCellReuseIdentifier:NSStringFromClass([RightDetailViewCell class])];
    
    self.datePickerView = [[DatePickerView alloc] init];
    _datePickerView.delegate = self;
    _datePickerView.isRightDetailDate = YES;
    [self addSubview:_datePickerView];
    [_datePickerView hide];
    
}




- (void)popDateViewAction:(UIButton *)button {
    [_datePickerView show];
}

- (void)cancelButtonAction {
    [_datePickerView hide];
}


- (void)rightDetailActionWithBeginTime:(NSString *)beginTime endTime:(NSString *)endTime {
    [self.datePickerDic setObject:beginTime forKey:@"beginTime"];
    [self.datePickerDic setObject:endTime forKey:@"endTime"];

    [self.beginDateButton setTitle:[beginTime substringFromIndex:5] forState:UIControlStateNormal];
    [self.endDateButton setTitle:[endTime substringFromIndex:5] forState:UIControlStateNormal];
    [self.datePickerView hide];
    
    // tableView变化
    NSDate *firstDate = [[CalendarTool shareCalendarTool] dateFromString:beginTime];
    NSDate *endDate = [[CalendarTool shareCalendarTool] dateFromString:endTime];
    
    _beginDate = [[CalendarTool shareCalendarTool] beginDateFromSmallStringDate:firstDate];
    _endDate = [[CalendarTool shareCalendarTool] endDateFromSmallStringDate:endDate];
    [self createData];

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return _rightDetailArr.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    RightDetailModel *model = _rightDetailArr[section];
    return model.day;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [CalculateFrameView setHeight:60];
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    RightDetailModel *model = _rightDetailArr[section];
    return model.dayArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RightDetailViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([RightDetailViewCell class]) forIndexPath:indexPath];
    RightDetailModel *model = _rightDetailArr[indexPath.section];
    RightDayModel *dayModel = model.dayArr[indexPath.row];
    cell.picImageView.image = [UIImage imageNamed:dayModel.picName];
    cell.nameLabel.text = dayModel.name;
    cell.moneyLabel.text = dayModel.money;
    return cell;
}

#pragma cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return [CalculateFrameView setHeight:30];
}

#pragma mark - section内容
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *sectionView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MAIN_WIDTH, [CalculateFrameView setHeight:30])];
    sectionView.backgroundColor = LIGHT_GRAY_SECTION;
    
    RightDetailModel *model = _rightDetailArr[section];

    
    self.sectionDateLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake([CalculateFrameView setWidth:20], [CalculateFrameView setHeight:5], 100 / 414.0 * MAIN_WIDTH, 20 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentLeft;
        [tempLabel setText:model.day];
        tempLabel.font = [UIFont systemFontOfSize:14];
        tempLabel.textColor = TEXT_COLOR;
        tempLabel;
    });
    
    self.sectionCountLabel = ({
        UILabel *tempLabel = [[UILabel alloc] initWithFrame:CGRectMake(MAIN_WIDTH - [CalculateFrameView setWidth:170], [CalculateFrameView setHeight:5], 150 / 414.0 * MAIN_WIDTH, 20 / 414.0 * MAIN_WIDTH)];
        [sectionView addSubview:tempLabel];
        tempLabel.textAlignment = NSTextAlignmentRight;
        [tempLabel setText:[NSString stringWithFormat:@"总额: %@", model.amount]];
        tempLabel.font = [UIFont systemFontOfSize:14];
        tempLabel.textColor = TEXT_COLOR;
        tempLabel;
    });
    

    return sectionView;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];

    RightDetailModel *model = _rightDetailArr[indexPath.section];
    RightDayModel *dayModel = model.dayArr[indexPath.row];
    [self.rightDetailDelegate pushToIntroduceViewControllerWithModel:dayModel];
}



- (void)clickBack:(UIButton *)button {
    [self.rightDetailDelegate popViewController];
}


@end
